/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package domain.history;

import domain.DomainController;
import domain.forumdata.Post;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 *
 * Contains a collection of history entries.
 */
public class History implements Serializable
{
    private ArrayList<Post> history;
    private char[] protection;

    /**
     * Default constructor. Initializes a new, empty list.
     */
    public History()
    {
        this.history = new ArrayList<Post>();
        this.protection = null;
    }

    /**
     * @return The history collection.
     */
    public ArrayList<Post> getHistory()
    {
        if(DomainController.getInstance().isLocked())
            return new ArrayList<Post>();
        
        return history;
    }

    /**
     * Searches if there's an exact same post availble. (Except for the date and topic URLs.)
     * @param p The parameters.
     * @return The post object if there is one. null if p is unique.
     */
    public Post getExactPost(Post p)
    {
        for(Post op : this.history)
        {
            if(op.getTopicTitle().equals(p.getTopicTitle()) &&
                    op.getTopicSubtitle().equals(p.getTopicSubtitle()) &&
                    op.getPost().equals(p.getPost()) &&
                    op.getTag().getTagName().equals(p.getTag().getTagName()) &&
                    op.getCat().getName().equals( p.getCat().getName()))
            {
                //We have a match!
                return op;
            }
        }
        
        return null;
    }

    /**
     * @param text text that needs to occur.
     * @return The filtered list.
     */
    public List<Post> getHistoryWithSearch(String text)
    {
        if(DomainController.getInstance().isLocked())
            return new ArrayList<Post>();
        
        ArrayList<Post> posts = new ArrayList<Post>();

        for(Post post : this.history)
        {
            if(post.containsText(text.toLowerCase()))
                posts.add(post);
        }

        return posts;
    }

    public char[] getProtection()
    {
        return protection;
    }

    public void setProtection(char[] protection)
    {
        this.protection = protection;
    }
}